package cn.ld.module.system.dal.mapper.logger;

import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.mybatis.core.mapper.BaseMapperX;
import cn.ld.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ld.module.system.controller.admin.logger.vo.loginlog.LoginLogPageInVO;
import cn.ld.module.system.dal.entity.logger.LoginLogEntity;
import cn.ld.module.system.enums.logger.LoginResultEnum;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface LoginLogMapper extends BaseMapperX<LoginLogEntity> {

    default PageResult<LoginLogEntity> selectPage(LoginLogPageInVO inVO) {
        LambdaQueryWrapperX<LoginLogEntity> query = new LambdaQueryWrapperX<LoginLogEntity>()
                .likeIfPresent(LoginLogEntity::getUserIp, inVO.getUserIp())
                .likeIfPresent(LoginLogEntity::getUsername, inVO.getUsername())
                .betweenIfPresent(LoginLogEntity::getCreateTime, inVO.getCreateTime());
        if (Boolean.TRUE.equals(inVO.getStatus())) {
            query.eq(LoginLogEntity::getResult, LoginResultEnum.SUCCESS.getResult());
        } else if (Boolean.FALSE.equals(inVO.getStatus())) {
            query.gt(LoginLogEntity::getResult, LoginResultEnum.SUCCESS.getResult());
        }
        query.orderByDesc(LoginLogEntity::getId); // 降序
        return selectPage(inVO, query);
    }

}
